
********************************************************************************
* Master replication file
* Paper: Janusz & Vallejo (2026), Political Research Quarterly
* Purpose: main analysis, robustness checks, and sensitivity figures
*
* Before running this file:
*   1. Place the raw dataset "dataMunis.dta" inside the folder /data
*   2. Ensure required user-written commands are installed:
*      rdrobust, rd, grc1leg2, rdpow, rdhte
*   3. This file builds "dataMunis_analysis_ready.dta" in /data and writes all tables and figures to the project subfolders
********************************************************************************

version 18.0
clear all
set more off
set linesize 255
capture log close

********************************************************************************
* 1. Project folders
********************************************************************************

if c(os) == "Windows" {
    global root "C:/Users/`c(username)'/OneDrive/Desktop/JanuszVallejo2026_PRQ"
}
else if c(os) == "MacOSX" {
    global root "/Users/`c(username)'/OneDrive/Desktop/JanuszVallejo2026_PRQ"
}
else if c(os) == "Unix" {
    global root "/home/`c(username)'/OneDrive/Desktop/JanuszVallejo2026_PRQ"
}
else {
    global root "`c(pwd)'/JanuszVallejo2026_PRQ"
}

capture mkdir "$root"
capture mkdir "$root/data"
capture mkdir "$root/output"
capture mkdir "$root/figures"
capture mkdir "$root/tables"

cd "$root"

log using "output/master_replication.log", replace text

********************************************************************************
* 2. Required commands
********************************************************************************

capture which rdrobust
if _rc {
    di as error "The user-written command rdrobust is required but not installed."
    di as error "Install rdrobust and re-run this file."
    exit 199
}

capture which rd
if _rc {
    di as error "The user-written command rd is required but not installed."
    di as error "Install rd and re-run this file."
    exit 199
}

capture which grc1leg2
if _rc {
    di as error "The user-written command grc1leg2 is required but not installed."
    di as error "Install grc1leg2 and re-run this file."
    exit 199
}

capture which rdpow
if _rc {
    di as error "The user-written command rdpow is required but not installed."
    di as error "Install rdpow and re-run this file."
    exit 199
}

capture which rdhte
if _rc {
    di as error "The user-written command rdhte is required but not installed."
    di as error "Install rdhte and re-run this file."
    exit 199
}

set scheme s1color

********************************************************************************
* 3. Build analysis-ready dataset
********************************************************************************

* Done in separate file

** Choose the data file 
use "~\OneDrive\Desktop\JanuszVallejo2026_PRQ\data\dataMunis_analysis_ready.dta"


********************************************************************************
* 4. Main analysis
********************************************************************************

graph close _all

********************************************************************************
* 4A. RD plots for rerunning by office and race
********************************************************************************

quietly rdplot y_rerun_100 margin if office=="PREFEITO" & newcomer==1, ///
    p(4) graph_options(nodraw) vce(cluster cluster)
local tau = string(e(coef_r)[1,1] - e(coef_l)[1,1], "%4.2f")
quietly rdplot y_rerun_100 margin if office=="PREFEITO" & newcomer==1, ///
    p(4) nbins(30 30) vce(cluster cluster) ///
    graph_options( ///
    title("(a) All mayoral candidates (`e(N_l)' | `e(N_r)')", box bexpand size(medium) fcolor(ltblue)) ///
    subtitle("tau = `tau'", box bexpand fcolor(white) size(medlarge)) ///
    legend(off) ///
    ylabel(0(20)100, grid labsize(small) angle(0)) ///
    xlabel(-100(20)100, grid labsize(small)) ///
    yscale(range(0 100)) ///
    xscale(range(-100 102)) ///
    graphregion(margin(0 1 0 0)) ///
    plotregion(margin(0 0 0 0)) ///
    name(g_mayor_all, replace))

quietly rdplot y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, ///
    p(4) graph_options(nodraw) vce(cluster cluster)
local tau = string(e(coef_r)[1,1] - e(coef_l)[1,1], "%4.2f")
quietly rdplot y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, ///
    p(4) nbins(30 30) vce(cluster cluster) ///
    graph_options( ///
    title("(b) White mayoral candidates (`e(N_l)' | `e(N_r)')", box bexpand size(medium) fcolor(ltblue)) ///
    subtitle("tau = `tau'", box bexpand fcolor(white) size(medlarge)) ///
    legend(off) ///
    ylabel(0(20)100, grid labsize(small) angle(0)) ///
    xlabel(-100(20)100, grid labsize(small)) ///
    yscale(range(0 100)) ///
    xscale(range(-100 102)) ///
    graphregion(margin(0 1 0 0)) ///
    plotregion(margin(0 0 0 0)) ///
    name(g_mayor_white, replace))

quietly rdplot y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, ///
    p(4) graph_options(nodraw) vce(cluster cluster)
local tau = string(e(coef_r)[1,1] - e(coef_l)[1,1], "%4.2f")
quietly rdplot y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, ///
    p(4) nbins(30 30) vce(cluster cluster) ///
    graph_options( ///
    title("(c) Afro-Brazilian mayoral candidates (`e(N_l)' | `e(N_r)')", box bexpand size(medium) fcolor(ltblue)) ///
    subtitle("tau = `tau'", box bexpand fcolor(white) size(medlarge)) ///
    legend(off) ///
    ylabel(0(20)100, grid labsize(small) angle(0)) ///
    xlabel(-100(20)100, grid labsize(small)) ///
    yscale(range(0 100)) ///
    xscale(range(-100 102)) ///
    graphregion(margin(0 1 0 0)) ///
    plotregion(margin(0 0 0 0)) ///
    name(g_mayor_black, replace))

quietly rdplot y_rerun_100 margin if office=="VEREADOR" & newcomer==1, ///
    p(4) graph_options(nodraw) vce(cluster cluster)
local tau = string(e(coef_r)[1,1] - e(coef_l)[1,1], "%4.2f")
quietly rdplot y_rerun_100 margin if office=="VEREADOR" & newcomer==1, ///
    p(4) nbins(30 30) vce(cluster cluster) ///
    graph_options( ///
    title("(d) All city council candidates (`e(N_l)' | `e(N_r)')", box bexpand size(medium) fcolor(ltblue)) ///
    subtitle("tau = `tau'", box bexpand fcolor(white) size(medlarge)) ///
    legend(off) ///
    ylabel(0(20)100, grid labsize(small) angle(0)) ///
    xlabel(-20(5)20, grid labsize(small)) ///
    yscale(range(0 100)) ///
    xscale(range(-20 21)) ///
    graphregion(margin(0 1 0 0)) ///
    plotregion(margin(0 0 0 0)) ///
    name(g_council_all, replace))

quietly rdplot y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, ///
    p(4) graph_options(nodraw) vce(cluster cluster)
local tau = string(e(coef_r)[1,1] - e(coef_l)[1,1], "%4.2f")
quietly rdplot y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, ///
    p(4) nbins(30 30) vce(cluster cluster) ///
    graph_options( ///
    title("(e) White city council candidates (`e(N_l)' | `e(N_r)')", box bexpand size(medium) fcolor(ltblue)) ///
    subtitle("tau = `tau'", box bexpand fcolor(white) size(medlarge)) ///
    legend(off) ///
    ylabel(0(20)100, grid labsize(small) angle(0)) ///
    xlabel(-20(5)20, grid labsize(small)) ///
    yscale(range(0 100)) ///
    xscale(range(-20 21)) ///
    graphregion(margin(0 1 0 0)) ///
    plotregion(margin(0 0 0 0)) ///
    name(g_council_white, replace))

quietly rdplot y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, ///
    p(4) graph_options(nodraw) vce(cluster cluster)
local tau = string(e(coef_r)[1,1] - e(coef_l)[1,1], "%4.2f")
quietly rdplot y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, ///
    p(4) nbins(30 30) vce(cluster cluster) ///
    graph_options( ///
    title("(f) Afro-Brazilian city council candidates (`e(N_l)' | `e(N_r)')", box bexpand size(medium) fcolor(ltblue)) ///
    subtitle("tau = `tau'", box bexpand fcolor(white) size(medlarge)) ///
    legend(off) ///
    ylabel(0(20)100, grid labsize(small) angle(0)) ///
    xlabel(-20(5)20, grid labsize(small)) ///
    yscale(range(0 100)) ///
    xscale(range(-20 21)) ///
    graphregion(margin(0 1 0 0)) ///
    plotregion(margin(0 0 0 0)) ///
    name(g_council_black, replace))

graph combine g_mayor_all g_mayor_white g_mayor_black, col(3) ///
    graphregion(margin(0 0 0 0)) plotregion(margin(0 0 0 0)) ///
    title("Mayoral candidates", size(large)) name(g_mayor_panel, replace)

graph combine g_council_all g_council_white g_council_black, col(3) ///
    graphregion(margin(0 0 0 0)) plotregion(margin(0 0 0 0)) ///
    title("City council candidates", size(large)) name(g_council_panel, replace)

graph combine g_mayor_panel g_council_panel, col(1) ///
    graphregion(margin(0 0 0 0)) plotregion(margin(0 0 0 0)) ///
    name(figure_main_rdd, replace)

graph display figure_main_rdd, ysize(4)
graph export "figures/figure_rdd_rerunning_by_office_and_race.pdf", replace
graph save "figures/figure_rdd_rerunning_by_office_and_race.gph", replace

********************************************************************************
* 4B. Main RD estimates table
********************************************************************************

putexcel set "tables/table_main_rerunning_by_office.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


* The legacy "observations used" block from the original script is omitted here
* because it relied on a variable (lwfl) that is not part of the final analysis file.
* Omitting that block makes the replication script cleaner and fully transparent.



********************************************************************************
* 5. Appendix robustness checks
********************************************************************************

graph close _all

********************************************************************************
* 5A. Coverage error rate optimal bandwidth
********************************************************************************
putexcel set "tables/app_coverage_error_rate.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1, vce(cluster cluster) bwselect(cerrd)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) bwselect(cerrd)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) bwselect(cerrd)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1, vce(cluster cluster) bwselect(cerrd)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) bwselect(cerrd)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) bwselect(cerrd)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5B. Second-order polynomial
********************************************************************************
putexcel set "tables/app_polynomial_order_2.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1, vce(cluster cluster) p(2)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) p(2)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) p(2)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1, vce(cluster cluster) p(2)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) p(2)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) p(2)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5C. Third-order polynomial
********************************************************************************
putexcel set "tables/app_polynomial_order_3.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1, vce(cluster cluster) p(3)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) p(3)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) p(3)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1, vce(cluster cluster) p(3)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) p(3)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) p(3)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5D. RD estimates with controls
********************************************************************************
putexcel set "tables/app_controls.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1, vce(cluster cluster) covs(female age married college)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) covs(female age married college)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) covs(female age married college)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1, vce(cluster cluster) covs(female age married pm dm college)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, vce(cluster cluster) covs(female age married pm dm college)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, vce(cluster cluster) covs(female age married pm dm college)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5E. Full sample, not only newcomers
********************************************************************************
putexcel set "tables/app_full_sample_not_only_newcomers.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO", vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & blackBrazilian==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & blackBrazilian==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR", vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & blackBrazilian==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & blackBrazilian==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5F. Same-party rerunning outcome
********************************************************************************
putexcel set "tables/table_same_party_rerunning_by_office.xlsx", replace
putexcel A1=("Outcome: rerunning for same party (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_sameparty_100 margin if office=="PREFEITO" & newcomer==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_sameparty_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_sameparty_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_sameparty_100 margin if office=="VEREADOR" & newcomer==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_sameparty_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_sameparty_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5G. Biquadratic specification
********************************************************************************


putexcel set "tables/app_BIQUADRATIC.xlsx", replace
putexcel A1=("dv: rerunning (0/100)"), bold
putexcel B1=("Estimation"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- — N+"), bold

putexcel A2=("Mayoral Candidates"), bold
putexcel A3=("All"), bold
putexcel A4=("White"), bold
putexcel A5=("Afro-Brazilian"), bold
putexcel A6=("City Council Candidates"), bold
putexcel A7=("All"), bold
putexcel A8=("White"), bold
putexcel A9=("Afro-Brazilian"), bold

quietly regress y_rerun_100 i.incumbent##c.margin##c.margin##c.margin##c.margin ///
    if newcomer==1 & office=="PREFEITO", vce(cluster cluster)
quietly lincom 1.incumbent
local estimate = round(r(estimate), .01)
local ci_l = round(r(lb), .01)
local ci_r = round(r(ub), .01)
local p_value = round(r(p), .01)
quietly summarize margin if newcomer==1 & office=="PREFEITO"
local bw = round(r(max), .01)
quietly count if newcomer==1 & office=="PREFEITO" & incumbent==0
local n_left = r(N)
quietly count if newcomer==1 & office=="PREFEITO" & incumbent==1
local n_right = r(N)
putexcel B3=(`estimate'), nformat(number_d2)
putexcel C3=("(`ci_l' — `ci_r')")
putexcel D3=(`p_value'), nformat(number_d2)
putexcel E3=(`bw'), nformat(number_d2)
putexcel F3=("(`n_left' — `n_right')")

quietly regress y_rerun_100 i.incumbent##c.margin##c.margin##c.margin##c.margin ///
    if newcomer==1 & office=="PREFEITO" & blackBrazilian==0, vce(cluster cluster)
quietly lincom 1.incumbent
local estimate = round(r(estimate), .01)
local ci_l = round(r(lb), .01)
local ci_r = round(r(ub), .01)
local p_value = round(r(p), .01)
quietly summarize margin if newcomer==1 & office=="PREFEITO" & blackBrazilian==0
local bw = round(r(max), .01)
quietly count if newcomer==1 & office=="PREFEITO" & blackBrazilian==0 & incumbent==0
local n_left = r(N)
quietly count if newcomer==1 & office=="PREFEITO" & blackBrazilian==0 & incumbent==1
local n_right = r(N)
putexcel B4=(`estimate'), nformat(number_d2)
putexcel C4=("(`ci_l' — `ci_r')")
putexcel D4=(`p_value'), nformat(number_d2)
putexcel E4=(`bw'), nformat(number_d2)
putexcel F4=("(`n_left' — `n_right')")

quietly regress y_rerun_100 i.incumbent##c.margin##c.margin##c.margin##c.margin ///
    if newcomer==1 & office=="PREFEITO" & blackBrazilian==1, vce(cluster cluster)
quietly lincom 1.incumbent
local estimate = round(r(estimate), .01)
local ci_l = round(r(lb), .01)
local ci_r = round(r(ub), .01)
local p_value = round(r(p), .01)
quietly summarize margin if newcomer==1 & office=="PREFEITO" & blackBrazilian==1
local bw = round(r(max), .01)
quietly count if newcomer==1 & office=="PREFEITO" & blackBrazilian==1 & incumbent==0
local n_left = r(N)
quietly count if newcomer==1 & office=="PREFEITO" & blackBrazilian==1 & incumbent==1
local n_right = r(N)
putexcel B5=(`estimate'), nformat(number_d2)
putexcel C5=("(`ci_l' — `ci_r')")
putexcel D5=(`p_value'), nformat(number_d2)
putexcel E5=(`bw'), nformat(number_d2)
putexcel F5=("(`n_left' — `n_right')")

quietly regress y_rerun_100 i.incumbent##c.margin##c.margin##c.margin##c.margin ///
    if newcomer==1 & office=="VEREADOR", vce(cluster cluster)
quietly lincom 1.incumbent
local estimate = round(r(estimate), .01)
local ci_l = round(r(lb), .01)
local ci_r = round(r(ub), .01)
local p_value = round(r(p), .01)
quietly summarize margin if newcomer==1 & office=="VEREADOR"
local bw = round(r(max), .01)
quietly count if newcomer==1 & office=="VEREADOR" & incumbent==0
local n_left = r(N)
quietly count if newcomer==1 & office=="VEREADOR" & incumbent==1
local n_right = r(N)
putexcel B7=(`estimate'), nformat(number_d2)
putexcel C7=("(`ci_l' — `ci_r')")
putexcel D7=(`p_value'), nformat(number_d2)
putexcel E7=(`bw'), nformat(number_d2)
putexcel F7=("(`n_left' — `n_right')")

quietly regress y_rerun_100 i.incumbent##c.margin##c.margin##c.margin##c.margin ///
    if newcomer==1 & office=="VEREADOR" & blackBrazilian==0, vce(cluster cluster)
quietly lincom 1.incumbent
local estimate = round(r(estimate), .01)
local ci_l = round(r(lb), .01)
local ci_r = round(r(ub), .01)
local p_value = round(r(p), .01)
quietly summarize margin if newcomer==1 & office=="VEREADOR" & blackBrazilian==0
local bw = round(r(max), .01)
quietly count if newcomer==1 & office=="VEREADOR" & blackBrazilian==0 & incumbent==0
local n_left = r(N)
quietly count if newcomer==1 & office=="VEREADOR" & blackBrazilian==0 & incumbent==1
local n_right = r(N)
putexcel B8=(`estimate'), nformat(number_d2)
putexcel C8=("(`ci_l' — `ci_r')")
putexcel D8=(`p_value'), nformat(number_d2)
putexcel E8=(`bw'), nformat(number_d2)
putexcel F8=("(`n_left' — `n_right')")

quietly regress y_rerun_100 i.incumbent##c.margin##c.margin##c.margin##c.margin ///
    if newcomer==1 & office=="VEREADOR" & blackBrazilian==1, vce(cluster cluster)
quietly lincom 1.incumbent
local estimate = round(r(estimate), .01)
local ci_l = round(r(lb), .01)
local ci_r = round(r(ub), .01)
local p_value = round(r(p), .01)
quietly summarize margin if newcomer==1 & office=="VEREADOR" & blackBrazilian==1
local bw = round(r(max), .01)
quietly count if newcomer==1 & office=="VEREADOR" & blackBrazilian==1 & incumbent==0
local n_left = r(N)
quietly count if newcomer==1 & office=="VEREADOR" & blackBrazilian==1 & incumbent==1
local n_right = r(N)
putexcel B9=(`estimate'), nformat(number_d2)
putexcel C9=("(`ci_l' — `ci_r')")
putexcel D9=(`p_value'), nformat(number_d2)
putexcel E9=(`bw'), nformat(number_d2)
putexcel F9=("(`n_left' — `n_right')")


********************************************************************************
* 5H. Year-specific estimates: 2016
********************************************************************************
putexcel set "tables/app_year_2016.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & year==2016, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0 & year==2016, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1 & year==2016, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & year==2016, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0 & year==2016, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1 & year==2016, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5I. Year-specific estimates: 2020
********************************************************************************
putexcel set "tables/app_year_2020.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Mayoral Candidates"), bold
putexcel A3=("White Mayoral Candidates"), bold
putexcel A4=("Afro-Brazilian Mayoral Candidates"), bold
putexcel A5=("All City Council Candidates"), bold
putexcel A6=("White City Council Candidates"), bold
putexcel A7=("Afro-Brazilian City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & year==2020, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0 & year==2020, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1 & year==2020, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & year==2020, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0 & year==2020, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1 & year==2020, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5J. Disaggregated race categories
********************************************************************************
putexcel set "tables/app_black_subcategories.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("Preta Mayoral Candidates"), bold
putexcel A3=("Parda Mayoral Candidates"), bold
putexcel A4=("Preta City Council Candidates"), bold
putexcel A5=("Parda City Council Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & ds_cor_raca=="PRETA", vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & ds_cor_raca=="PARDA", vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & ds_cor_raca=="PRETA", vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & ds_cor_raca=="PARDA", vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5K. Gender heterogeneity: city council
********************************************************************************
putexcel set "tables/app_gender_city_council.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Male Candidates"), bold
putexcel A3=("All Female Candidates"), bold
putexcel A4=("White Male Candidates"), bold
putexcel A5=("Afro-Brazilian Male Candidates"), bold
putexcel A6=("White Female Candidates"), bold
putexcel A7=("Afro-Brazilian Female Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & female==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & female==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0 & female==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1 & female==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0 & female==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1 & female==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5L. Gender heterogeneity: mayoral
********************************************************************************
putexcel set "tables/app_gender_mayoral.xlsx", replace
putexcel A1=("Outcome: rerunning (0-100)"), bold
putexcel B1=("Estimate"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- | N+"), bold
putexcel A2=("All Male Candidates"), bold
putexcel A3=("All Female Candidates"), bold
putexcel A4=("White Male Candidates"), bold
putexcel A5=("Afro-Brazilian Male Candidates"), bold
putexcel A6=("White Female Candidates"), bold
putexcel A7=("Afro-Brazilian Female Candidates"), bold

quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & female==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B2=(e(tau_cl)), nformat(number_d2)
putexcel C2="(`ci_l' to `ci_r')"
putexcel D2=(e(pv_rb)), nformat(number_d2)
putexcel E2=(e(h_r)), nformat(number_d2)
putexcel F2="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & female==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B3=(e(tau_cl)), nformat(number_d2)
putexcel C3="(`ci_l' to `ci_r')"
putexcel D3=(e(pv_rb)), nformat(number_d2)
putexcel E3=(e(h_r)), nformat(number_d2)
putexcel F3="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0 & female==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B4=(e(tau_cl)), nformat(number_d2)
putexcel C4="(`ci_l' to `ci_r')"
putexcel D4=(e(pv_rb)), nformat(number_d2)
putexcel E4=(e(h_r)), nformat(number_d2)
putexcel F4="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1 & female==0, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B5=(e(tau_cl)), nformat(number_d2)
putexcel C5="(`ci_l' to `ci_r')"
putexcel D5=(e(pv_rb)), nformat(number_d2)
putexcel E5=(e(h_r)), nformat(number_d2)
putexcel F5="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0 & female==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B6=(e(tau_cl)), nformat(number_d2)
putexcel C6="(`ci_l' to `ci_r')"
putexcel D6=(e(pv_rb)), nformat(number_d2)
putexcel E6=(e(h_r)), nformat(number_d2)
putexcel F6="(`e(N_h_l)' | `e(N_h_r)')"


quietly rdrobust y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1 & female==1, vce(cluster cluster)
local ci_l = round(e(ci_l_rb), .01)
local ci_r = round(e(ci_r_rb), .01)
putexcel B7=(e(tau_cl)), nformat(number_d2)
putexcel C7="(`ci_l' to `ci_r')"
putexcel D7=(e(pv_rb)), nformat(number_d2)
putexcel E7=(e(h_r)), nformat(number_d2)
putexcel F7="(`e(N_h_l)' | `e(N_h_r)')"


********************************************************************************
* 5M. Power analysis
********************************************************************************


putexcel set "tables/power_analyses.xlsx", replace
putexcel A1=("Group"), bold
putexcel B1=("Tau"), bold
putexcel C1=("BW"), bold
putexcel D1=("N- — N+"), bold
putexcel E1=("Power (0.2 tau)"), bold
putexcel F1=("Power (0.5 tau)"), bold
putexcel G1=("Power (tau)"), bold

putexcel A2=("Mayoral Candidates"), bold
putexcel A3=("All"), bold
putexcel A4=("White"), bold
putexcel A5=("Afro-Brazilian"), bold
putexcel A6=("City Council Candidates"), bold
putexcel A7=("All"), bold
putexcel A8=("White"), bold
putexcel A9=("Afro-Brazilian"), bold

* Tau inputs below follow the original powerAnalysis.do file.
quietly rdpow y_rerun_100 margin if office=="PREFEITO" & newcomer==1, tau(20.45)
local power20 = 1 - normal((0.2*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.2*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local power50 = 1 - normal((0.5*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.5*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local n_left = r(N_h_l)
local n_right = r(N_h_r)
putexcel B3=(r(tau)), nformat(number_d2)
putexcel C3=(r(samph_r)), nformat(number_d2)
putexcel D3=("(`n_left' — `n_right')")
putexcel E3=(`power20'), nformat(number_d3)
putexcel F3=(`power50'), nformat(number_d3)
putexcel G3=(r(power_rbc)), nformat(number_d3)

quietly rdpow y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==0, tau(21.51)
local power20 = 1 - normal((0.2*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.2*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local power50 = 1 - normal((0.5*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.5*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local n_left = r(N_h_l)
local n_right = r(N_h_r)
putexcel B4=(r(tau)), nformat(number_d2)
putexcel C4=(r(samph_r)), nformat(number_d2)
putexcel D4=("(`n_left' — `n_right')")
putexcel E4=(`power20'), nformat(number_d3)
putexcel F4=(`power50'), nformat(number_d3)
putexcel G4=(r(power_rbc)), nformat(number_d3)

quietly rdpow y_rerun_100 margin if office=="PREFEITO" & newcomer==1 & blackBrazilian==1, tau(21.11)
local power20 = 1 - normal((0.2*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.2*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local power50 = 1 - normal((0.5*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.5*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local n_left = r(N_h_l)
local n_right = r(N_h_r)
putexcel B5=(r(tau)), nformat(number_d2)
putexcel C5=(r(samph_r)), nformat(number_d2)
putexcel D5=("(`n_left' — `n_right')")
putexcel E5=(`power20'), nformat(number_d3)
putexcel F5=(`power50'), nformat(number_d3)
putexcel G5=(r(power_rbc)), nformat(number_d3)

quietly rdpow y_rerun_100 margin if office=="VEREADOR" & newcomer==1, tau(13.86)
local power20 = 1 - normal((0.2*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.2*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local power50 = 1 - normal((0.5*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.5*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local n_left = r(N_h_l)
local n_right = r(N_h_r)
putexcel B7=(r(tau)), nformat(number_d2)
putexcel C7=(r(samph_r)), nformat(number_d2)
putexcel D7=("(`n_left' — `n_right')")
putexcel E7=(`power20'), nformat(number_d3)
putexcel F7=(`power50'), nformat(number_d3)
putexcel G7=(r(power_rbc)), nformat(number_d3)

quietly rdpow y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==0, tau(14.10)
local power20 = 1 - normal((0.2*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.2*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local power50 = 1 - normal((0.5*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.5*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local n_left = r(N_h_l)
local n_right = r(N_h_r)
putexcel B8=(r(tau)), nformat(number_d2)
putexcel C8=(r(samph_r)), nformat(number_d2)
putexcel D8=("(`n_left' — `n_right')")
putexcel E8=(`power20'), nformat(number_d3)
putexcel F8=(`power50'), nformat(number_d3)
putexcel G8=(r(power_rbc)), nformat(number_d3)

quietly rdpow y_rerun_100 margin if office=="VEREADOR" & newcomer==1 & blackBrazilian==1, tau(14.46)
local power20 = 1 - normal((0.2*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.2*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local power50 = 1 - normal((0.5*r(tau))/r(se_rbc) + invnormal(1-r(alpha)/2)) + normal((0.5*r(tau))/r(se_rbc) - invnormal(1-r(alpha)/2))
local n_left = r(N_h_l)
local n_right = r(N_h_r)
putexcel B9=(r(tau)), nformat(number_d2)
putexcel C9=(r(samph_r)), nformat(number_d2)
putexcel D9=("(`n_left' — `n_right')")
putexcel E9=(`power20'), nformat(number_d3)
putexcel F9=(`power50'), nformat(number_d3)
putexcel G9=(r(power_rbc)), nformat(number_d3)

********************************************************************************
* 5N. Heterogeneous treatment effects by race
********************************************************************************


putexcel set "tables/heterogenousEffect.xlsx", replace
putexcel A1=("dv: rerunning (0/100)"), bold
putexcel B1=("Estimation"), bold
putexcel C1=("95% CI"), bold
putexcel D1=("p-value"), bold
putexcel E1=("Bandwidth"), bold
putexcel F1=("N- — N+"), bold

putexcel A2=("Mayoral Candidates (newcomers)"), bold
putexcel A3=("White"), bold
putexcel A4=("Afro-Brazilian"), bold
putexcel A5=("City Council Candidates (newcomers)"), bold
putexcel A6=("White"), bold
putexcel A7=("Afro-Brazilian"), bold

quietly rdhte y_rerun_100 margin if office=="PREFEITO" & newcomer==1, ///
    covs_hte(i.blackBrazilian) vce(cluster cluster)

matrix hte_est = e(tau_hat)
if rowsof(hte_est) == 1 {
    matrix hte_est = hte_est'
}
matrix hte_pv = e(tau_pv)
if rowsof(hte_pv) == 1 {
    matrix hte_pv = hte_pv'
}
matrix hte_lb = e(tau_ci_lb)
if rowsof(hte_lb) == 1 {
    matrix hte_lb = hte_lb'
}
matrix hte_ub = e(tau_ci_ub)
if rowsof(hte_ub) == 1 {
    matrix hte_ub = hte_ub'
}
matrix hte_bw = e(h)
if rowsof(hte_bw) == 1 {
    matrix hte_bw = hte_bw'
}
matrix hte_n = e(tau_N)
if rowsof(hte_n) == 1 {
    matrix hte_n = hte_n'
}

* Row 1 corresponds to blackBrazilian==0 (White); row 2 corresponds to blackBrazilian==1.
local estimate = round(hte_est[1,1], .01)
local ci_l = round(hte_lb[1,1], .01)
local ci_r = round(hte_ub[1,1], .01)
local p_value = round(hte_pv[1,1], .01)
local bw = round(hte_bw[1,2], .01)
local n_left = hte_n[1,1]
local n_right = hte_n[1,2]
putexcel B3=(`estimate'), nformat(number_d2)
putexcel C3=("(`ci_l' — `ci_r')")
putexcel D3=(`p_value'), nformat(number_d2)
putexcel E3=(`bw'), nformat(number_d2)
putexcel F3=("(`n_left' — `n_right')")

local estimate = round(hte_est[2,1], .01)
local ci_l = round(hte_lb[2,1], .01)
local ci_r = round(hte_ub[2,1], .01)
local p_value = round(hte_pv[2,1], .01)
local bw = round(hte_bw[2,2], .01)
local n_left = hte_n[2,1]
local n_right = hte_n[2,2]
putexcel B4=(`estimate'), nformat(number_d2)
putexcel C4=("(`ci_l' — `ci_r')")
putexcel D4=(`p_value'), nformat(number_d2)
putexcel E4=(`bw'), nformat(number_d2)
putexcel F4=("(`n_left' — `n_right')")

quietly rdhte y_rerun_100 margin if office=="VEREADOR" & newcomer==1, ///
    covs_hte(i.blackBrazilian) vce(cluster cluster)

matrix hte_est = e(tau_hat)
if rowsof(hte_est) == 1 {
    matrix hte_est = hte_est'
}
matrix hte_pv = e(tau_pv)
if rowsof(hte_pv) == 1 {
    matrix hte_pv = hte_pv'
}
matrix hte_lb = e(tau_ci_lb)
if rowsof(hte_lb) == 1 {
    matrix hte_lb = hte_lb'
}
matrix hte_ub = e(tau_ci_ub)
if rowsof(hte_ub) == 1 {
    matrix hte_ub = hte_ub'
}
matrix hte_bw = e(h)
if rowsof(hte_bw) == 1 {
    matrix hte_bw = hte_bw'
}
matrix hte_n = e(tau_N)
if rowsof(hte_n) == 1 {
    matrix hte_n = hte_n'
}

local estimate = round(hte_est[1,1], .01)
local ci_l = round(hte_lb[1,1], .01)
local ci_r = round(hte_ub[1,1], .01)
local p_value = round(hte_pv[1,1], .01)
local bw = round(hte_bw[1,2], .01)
local n_left = hte_n[1,1]
local n_right = hte_n[1,2]
putexcel B6=(`estimate'), nformat(number_d2)
putexcel C6=("(`ci_l' — `ci_r')")
putexcel D6=(`p_value'), nformat(number_d2)
putexcel E6=(`bw'), nformat(number_d2)
putexcel F6=("(`n_left' — `n_right')")

local estimate = round(hte_est[2,1], .01)
local ci_l = round(hte_lb[2,1], .01)
local ci_r = round(hte_ub[2,1], .01)
local p_value = round(hte_pv[2,1], .01)
local bw = round(hte_bw[2,2], .01)
local n_left = hte_n[2,1]
local n_right = hte_n[2,2]
putexcel B7=(`estimate'), nformat(number_d2)
putexcel C7=("(`ci_l' — `ci_r')")
putexcel D7=(`p_value'), nformat(number_d2)
putexcel E7=(`bw'), nformat(number_d2)
putexcel F7=("(`n_left' — `n_right')")



********************************************************************************
* 7. Finish
********************************************************************************

log close
display as text "Replication complete."
display as text "Tables saved in:  $root/tables"
display as text "Figures saved in: $root/figures"
display as text "Analysis-ready data saved in: $root/data/dataMunis_analysis_ready.dta"
display as text "Derived sensitivity datasets and log saved in: $root/output"
